package com.fht.service.food.impl;

import com.fht.constant.ManagermentSystemConstant;
import com.fht.entity.FoodSpecification;
import com.fht.entity.FoodTasteCategory;
import com.fht.enums.ManagermentSystemEnum;
import com.fht.exception.ManagermentSystemException;
import com.fht.mapper.food.FoodSpecificationMapper;
import com.fht.service.food.FoodSpecificationService;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FoodSpecificationServiceImpl implements FoodSpecificationService {

    @Resource
    public FoodSpecificationMapper foodSpecificationMapper;


    @Override
    public LayUITableVO<List<FoodSpecification>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        List<FoodSpecification> list = foodSpecificationMapper.findPage(map);

        PageInfo<FoodSpecification> pageInfo = new PageInfo<>(list);
        List<FoodSpecification> foodSpecificationList = pageInfo.getList();
        for (FoodSpecification foodSpecification : foodSpecificationList) {
            foodSpecification.setBasicsPrice(String.valueOf(Integer.valueOf(foodSpecification.getBasicsPrice())/ ManagermentSystemConstant.PRICE_TRANSITION));
            Example example = new Example(FoodSpecification.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("recordId",foodSpecification.getRecordId());
            List<FoodSpecification> foodSpecifications = foodSpecificationMapper.selectByExample(example);
            if (foodSpecifications.size()>0) {
                String names = "";
                String addPrices = "";
                for (FoodSpecification specification : foodSpecifications) {
                    names += specification.getName() + "/";
                    addPrices += specification.getAddPrice()/ManagermentSystemConstant.PRICE_TRANSITION+ "/";
                }
;                names = names.substring(0, names.lastIndexOf("/"));
                foodSpecification.setNames(names);
                addPrices = addPrices.substring(0, addPrices.lastIndexOf("/"));
                foodSpecification.setAddPrices(addPrices);
            }
        }

        LayUITableVO vo = new LayUITableVO();
        vo.setCount(pageInfo.getTotal());
        vo.setData(foodSpecificationList);


        return vo;
    }

    @Override
    public LayUITableVO<List<FoodSpecification>> selectByRecordId(String recordId) {

        Example example = new Example(FoodSpecification.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("recordId",recordId);
        List<FoodSpecification> foodSpecifications = foodSpecificationMapper.selectByExample(example);
        for (FoodSpecification foodSpecification : foodSpecifications) {
            foodSpecification.setAddPrices(String.valueOf(foodSpecification.getAddPrice()/ManagermentSystemConstant.PRICE_TRANSITION));
        }
        LayUITableVO vo = new LayUITableVO();
        vo.setData(foodSpecifications);
        return vo;
    }

    @Override
    @Transactional
    public void add(FoodSpecification foodSpecification) {

        Example example = new Example(FoodSpecification.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("recordId",foodSpecification.getRecordId());
        c.andEqualTo("name",foodSpecification.getName());
        List<FoodSpecification> foodSpecifications = foodSpecificationMapper.selectByExample(example);
        if (foodSpecifications.size()>0){

            throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
        }

        String addPriceString = foodSpecification.getAddPrices();
        foodSpecification.setAddPrice((int)( Double.valueOf(addPriceString)*ManagermentSystemConstant.PRICE_TRANSITION));

        foodSpecificationMapper.insertSelective(foodSpecification);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        foodSpecificationMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(FoodSpecification foodSpecification) {
        String addPriceString = foodSpecification.getAddPrices();
        foodSpecification.setAddPrice((int)( Double.valueOf(addPriceString)*ManagermentSystemConstant.PRICE_TRANSITION));

        foodSpecificationMapper.updateByPrimaryKeySelective(foodSpecification);
    }
}
